/*
https://leetcode.cn/problems/length-of-longest-subarray-with-at-most-k-frequency/
2958. 最多 K 个重复元素的最长子数组
*/
class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        unordered_map<int , int> unmap; int left = 0, ans = 0;
        for(int rigth = 0; rigth < nums.size(); rigth++){
            ++unmap[nums[rigth]];
            while(unmap[nums[rigth]] > k){ --unmap[nums[left++]];}
            ans = max(ans, rigth - left + 1 );
        }
        return ans;
    }
};